﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace MiniFox.Security
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static class SymmetricCryptoExtension
    {
        #region Rc2

        #region 加密
        /// <summary>
        /// Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source)
        {
            var crypt = new Rc2CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source, string key)
        {
            var crypt = new Rc2CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source, byte[] key)
        {
            var crypt = new Rc2CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new Rc2CryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new Rc2CryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new Rc2CryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source, string key, string iv)
        {
            var crypt = new Rc2CryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new Rc2CryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string Rc2Encrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new Rc2CryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        #endregion

        #region 解密
        /// <summary>
        /// Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source)
        {
            var crypt = new Rc2CryptoService();
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source, byte[] key)
        {
            var crypt = new Rc2CryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source, string key)
        {
            var crypt = new Rc2CryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new Rc2CryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new Rc2CryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new Rc2CryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source, string key, string iv)
        {
            var crypt = new Rc2CryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new Rc2CryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string Rc2Decrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new Rc2CryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        #endregion

        #endregion

        #region AES

        #region 加密
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source)
        {
            var crypt = new AesCryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行AES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, byte[] key)
        {
            var crypt = new AesCryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行AES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, string key)
        {
            var crypt = new AesCryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行AES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new AesCryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行AES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new AesCryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行AES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new AesCryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行AES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, string key, string iv)
        {
            var crypt = new AesCryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行AES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new AesCryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行AES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string AesEncrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new AesCryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        #endregion

        #region 解密
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source)
        {
            var crypt = new AesCryptoService();
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行AES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, byte[] key)
        {
            var crypt = new AesCryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行AES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, string key)
        {
            var crypt = new AesCryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行AES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new AesCryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行AES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new AesCryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行AES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new AesCryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行AES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, string key, string iv)
        {
            var crypt = new AesCryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行AES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new AesCryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行AES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string AesDecrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new AesCryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        #endregion

        #endregion

        #region DES

        #region 加密
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source)
        {
            var crypt = new DesCryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行DES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source, byte[] key)
        {
            var crypt = new DesCryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行DES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source, string key)
        {
            var crypt = new DesCryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行DES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new DesCryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行DES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new DesCryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new DesCryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source, string key, string iv)
        {
            var crypt = new DesCryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new DesCryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string DesEncrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new DesCryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        #endregion

        #region 解密
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source)
        {
            var crypt = new DesCryptoService();
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行DES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source, byte[] key)
        {
            var crypt = new DesCryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行DES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source, string key)
        {
            var crypt = new DesCryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行DES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new DesCryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行DES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new DesCryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new DesCryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source, string key, string iv)
        {
            var crypt = new DesCryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new DesCryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string DesDecrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new DesCryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        #endregion

        #endregion

        #region 3DES

        #region 加密
        /// <summary>
        /// 3DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source)
        {
            var crypt = new TripleDESCryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行3DES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source, byte[] key)
        {
            var crypt = new TripleDESCryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行3DES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source, string key)
        {
            var crypt = new TripleDESCryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行3DES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new TripleDESCryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行3DES加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new TripleDESCryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行3DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new TripleDESCryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行3DES加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source, string key, string iv)
        {
            var crypt = new TripleDESCryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行3DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new TripleDESCryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行3DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string TripleDesEncrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new TripleDESCryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        #endregion

        #region 解密
        /// <summary>
        /// 3DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source)
        {
            var crypt = new TripleDESCryptoService();
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行3DES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source, byte[] key)
        {
            var crypt = new TripleDESCryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行3DES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source, string key)
        {
            var crypt = new TripleDESCryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行3DES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new TripleDESCryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行3DES解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new TripleDESCryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行3DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new TripleDESCryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行3DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source, string key, string iv)
        {
            var crypt = new TripleDESCryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行3DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new TripleDESCryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行3DES解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        public static string TripleDesDecrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new TripleDESCryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        #endregion

        #endregion

        #region Rijndael

        #region 加密
        /// <summary>
        /// Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source)
        {
            var crypt = new RijndaelCryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source, string key)
        {
            var crypt = new RijndaelCryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source, byte[] key)
        {
            var crypt = new RijndaelCryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new RijndaelCryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2加密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new RijndaelCryptoService(key, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new RijndaelCryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source, string key, string iv)
        {
            var crypt = new RijndaelCryptoService(key, iv);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new RijndaelCryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelEncrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new RijndaelCryptoService(key, iv, paddingMode);
            return crypt.Encrypt(source);
        }
        #endregion

        #region 解密
        /// <summary>
        /// Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source)
        {
            var crypt = new RijndaelCryptoService();
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source, byte[] key)
        {
            var crypt = new RijndaelCryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source, string key)
        {
            var crypt = new RijndaelCryptoService(key);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source, byte[] key, PaddingMode paddingMode)
        {
            var crypt = new RijndaelCryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥进行Rc2解密(ECB模式)
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source, string key, PaddingMode paddingMode)
        {
            var crypt = new RijndaelCryptoService(key, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source, byte[] key, byte[] iv)
        {
            var crypt = new RijndaelCryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source, string key, string iv)
        {
            var crypt = new RijndaelCryptoService(key, iv);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source, byte[] key, byte[] iv, PaddingMode paddingMode)
        {
            var crypt = new RijndaelCryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        /// <summary>
        /// 使用指定密钥和向量进行Rc2解密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="key"></param>
        /// <param name="iv"></param>
        /// <param name="paddingMode"></param>
        /// <returns></returns>
        [Obsolete]
        public static string RijndaelDecrypt(this string source, string key, string iv, PaddingMode paddingMode)
        {
            var crypt = new RijndaelCryptoService(key, iv, paddingMode);
            return crypt.Decrypt(source);
        }
        #endregion

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    public static class HashCryptoExtension
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GenerateRandomCode(int length)
        {
            RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create();
            byte[] data = new byte[length / 2];
            randomNumberGenerator.GetBytes(data);
            string str = BitConverter.ToString(data).Replace("-", string.Empty).ToLower();
            return str;
        }
        #region md5
        /// <summary>
        /// MD5加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string MD5Encrypt(this string source)
        {
            var crypt = new MD5CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// MD5加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <returns>加密后的字符串</returns>
        public static byte[] MD5Encrypt(this byte[] source)
        {
            var crypt = new MD5CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// MD5的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的16位字符串</returns>
        public static string MD5EncryptToHexString(this string source)
        {
            var crypt = new MD5CryptoService();
            return crypt.EncryptToHexString(source);
        }
        #endregion

        #region SHA1

        /// <summary>
        /// SHA1加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string SHA1Encrypt(this string source)
        {
            var crypt = new SHA1CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHA1加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <returns>加密后的字符串</returns>
        public static byte[] SHA1Encrypt(this byte[] source)
        {
            var crypt = new SHA1CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHA1的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的16位字符串</returns>
        public static string SHA1EncryptToHexString(this string source)
        {
            var crypt = new SHA1CryptoService();
            return crypt.EncryptToHexString(source);
        }
        #endregion

        #region SHA256

        /// <summary>
        /// SHA256 加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string SHA256Encrypt(this string source)
        {
            var crypt = new SHA256CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHA256 加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <returns>加密后的字符串</returns>
        public static byte[] SHA256Encrypt(this byte[] source)
        {
            var crypt = new SHA256CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHA256 的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的16位字符串</returns>
        public static string SHA256EncryptToHexString(this string source)
        {
            var crypt = new SHA256CryptoService();
            return crypt.EncryptToHexString(source);
        }
        #endregion

        #region SHA384

        /// <summary>
        /// SHA384 加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string SHA384Encrypt(this string source)
        {
            var crypt = new SHA384CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHA384 加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <returns>加密后的字符串</returns>
        public static byte[] SHA384Encrypt(this byte[] source)
        {
            var crypt = new SHA384CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHA384 的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的16位字符串</returns>
        public static string SHA384EncryptToHexString(this string source)
        {
            var crypt = new SHA384CryptoService();
            return crypt.EncryptToHexString(source);
        }
        #endregion

        #region SHA512

        /// <summary>
        /// SHA512 加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string SHA512Encrypt(this string source)
        {
            var crypt = new SHA512CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHA512 加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <returns>加密后的字符串</returns>
        public static byte[] SHA512Encrypt(this byte[] source)
        {
            var crypt = new SHA512CryptoService();
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHA512 的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <returns>加密后的16位字符串</returns>
        public static string SHA512EncryptToHexString(this string source)
        {
            var crypt = new SHA512CryptoService();
            return crypt.EncryptToHexString(source);
        }
        #endregion

        #region HMACSHA1

        /// <summary>
        /// HMACSHA1 加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <param name="key"></param>
        /// <returns>加密后的字符串</returns>
        public static string HMACSHA1Encrypt(this string source, string key)
        {
            var crypt = new HMACSHA1CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// HMACSHA1 加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <param name="key"></param>
        /// <returns>加密后的字符串</returns>
        public static byte[] HMACSHA1Encrypt(this byte[] source, string key)
        {
            var crypt = new HMACSHA1CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// HMACSHA1 的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <param name="key"></param>
        /// <returns>加密后的16位字符串</returns>
        public static string HMACSHA1EncryptToHexString(this string source, string key)
        {
            var crypt = new HMACSHA1CryptoService(key);
            return crypt.EncryptToHexString(source);
        }
        #endregion

        #region HMACSHA256

        /// <summary>
        /// HMACSHA256 加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <param name="key"></param>
        /// <returns>加密后的字符串</returns>
        public static string HMACSHA256Encrypt(this string source, string key)
        {
            var crypt = new HMACSHA256CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// SHAHMACSHA256256 加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <param name="key"></param>
        /// <returns>加密后的字符串</returns>
        public static byte[] HMACSHA256Encrypt(this byte[] source, string key)
        {
            var crypt = new HMACSHA256CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// HMACSHA256 的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <param name="key"></param>
        /// <returns>加密后的16位字符串</returns>
        public static string HMACSHA256EncryptToHexString(this string source, string key)
        {
            var crypt = new HMACSHA256CryptoService(key);
            return crypt.EncryptToHexString(source);
        }
        #endregion

        #region HMACSHA384

        /// <summary>
        /// HMACSHA384 加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <param name="key"></param>
        /// <returns>加密后的字符串</returns>
        public static string HMACSHA384Encrypt(this string source, string key)
        {
            var crypt = new HMACSHA384CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// HMACSHA384 加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <param name="key"></param>
        /// <returns>加密后的字符串</returns>
        public static byte[] HMACSHA384Encrypt(this byte[] source, string key)
        {
            var crypt = new HMACSHA384CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// HMACSHA384 的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <param name="key"></param>
        /// <returns>加密后的16位字符串</returns>
        public static string HMACSHA384EncryptToHexString(this string source, string key)
        {
            var crypt = new HMACSHA384CryptoService(key);
            return crypt.EncryptToHexString(source);
        }
        #endregion

        #region HMACSHA512

        /// <summary>
        /// HMACSHA512 加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <param name="key"></param>
        /// <returns>加密后的字符串</returns>
        public static string HMACSHA512Encrypt(this string source, string key)
        {
            var crypt = new HMACSHA512CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// HMACSHA512 加密方法
        /// </summary>
        /// <param name="source">要加密的字节</param>
        /// <param name="key"></param>
        /// <returns>加密后的字符串</returns>
        public static byte[] HMACSHA512Encrypt(this byte[] source, string key)
        {
            var crypt = new HMACSHA512CryptoService(key);
            return crypt.Encrypt(source);
        }
        /// <summary>
        /// HMACSHA512 的16位加密方法
        /// </summary>
        /// <param name="source">要加密的字符串</param>
        /// <param name="key"></param>
        /// <returns>加密后的16位字符串</returns>
        public static string HMACSHA512EncryptToHexString(this string source, string key)
        {
            var crypt = new HMACSHA512CryptoService(key);
            return crypt.EncryptToHexString(source);
        }
        #endregion
    }
    /// <summary>
    /// 
    /// </summary>
    public static class AsymmetricCryptoExtension
    {
        #region RSA
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static byte[] RandomKeyBytesGenerate<T>() where T : SymmetricAlgorithm, new()
        {
            T alg = new T();
            alg.GenerateKey();
            return alg.Key;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string RandomKeyBase64StringGenerate<T>() where T : SymmetricAlgorithm, new()
        {
            byte[] keyBytes = RandomKeyBytesGenerate<T>();

            string keystr = Convert.ToBase64String(keyBytes);
            return keystr;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string RandomKeyHashStringGenerate<T>() where T : SymmetricAlgorithm, new()
        {
            byte[] keyBytes = RandomKeyBytesGenerate<T>();

            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < keyBytes.Length; i++)
            {
                sBuilder.Append(keyBytes[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
        /// <summary>
        /// 使用指定公钥(base64字符串)进行RSA加密
        /// </summary>
        /// <param name="source"></param>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public static string RSAEncryptWithX509PublicKey(this string source, string publicKey)
        {
            byte[] sourcebytes = Encoding.UTF8.GetBytes(source);
            byte[] keybytes = Convert.FromBase64String(publicKey);
            byte[] encryptedbytes = RSAEncryptWithX509PublicKey(sourcebytes, keybytes);

            return Convert.ToBase64String(encryptedbytes);
        }
        /// <summary>
        /// 使用指定公钥进行RSA加密
        /// </summary>
        /// <param name="sourceBytes"></param>
        /// <param name="keyBytes"></param>
        /// <returns></returns>
        public static byte[] RSAEncryptWithX509PublicKey(this byte[] sourceBytes, byte[] keyBytes)
        {
            var rsa = new RSACryptoServiceProvider();
            RSAParameters? param = keyBytes.DecodeX509PublicKey();

            if (param != null)
                rsa.ImportParameters(param.GetValueOrDefault());
            else
                throw new InvalidOperationException();
            byte[] encryptedbytes = rsa.Encrypt(sourceBytes, false);
            return encryptedbytes;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="x509PublicKeyBytes"></param>
        /// <returns></returns>
        public static Nullable<RSAParameters> DecodeX509PublicKey(this byte[] x509PublicKeyBytes)
        {
            byte[] SeqOID = { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 };

            MemoryStream ms = new MemoryStream(x509PublicKeyBytes);
            BinaryReader reader = new BinaryReader(ms);

            if (reader.ReadByte() == 0x30) //跳过 0x30段
                ReadASNLength(reader);
            else
                return null;

            int identifierSize = 0;
            if (reader.ReadByte() == 0x30)
                identifierSize = ReadASNLength(reader);
            else
                return null;

            if (reader.ReadByte() == 0x06)
            {
                int oidLength = ReadASNLength(reader);
                byte[] oidBytes = new byte[oidLength];
                reader.Read(oidBytes, 0, oidBytes.Length);
                if (!oidBytes.SequenceEqual(SeqOID))
                    return null;

                int remainingBytes = identifierSize - 2 - oidBytes.Length;
                reader.ReadBytes(remainingBytes);
            }

            if (reader.ReadByte() == 0x03)
            {
                ReadASNLength(reader);
                reader.ReadByte();
                if (reader.ReadByte() == 0x30)
                {
                    ReadASNLength(reader);
                    if (reader.ReadByte() == 0x02)
                    {
                        int modulusSize = ReadASNLength(reader);
                        byte[] modulus = new byte[modulusSize];
                        reader.Read(modulus, 0, modulus.Length);
                        if (modulus[0] == 0x00)
                        {
                            byte[] tempModulus = new byte[modulus.Length - 1];
                            Array.Copy(modulus, 1, tempModulus, 0, modulus.Length - 1);
                            modulus = tempModulus;
                        }

                        if (reader.ReadByte() == 0x02)
                        {
                            int exponentSize = ReadASNLength(reader);
                            byte[] exponent = new byte[exponentSize];
                            reader.Read(exponent, 0, exponent.Length);

                            RSAParameters RSAKeyInfo = new RSAParameters();
                            RSAKeyInfo.Modulus = modulus;
                            RSAKeyInfo.Exponent = exponent;
                            return RSAKeyInfo;
                        }
                    }
                }
            }
            return null;
        }

        private static int ReadASNLength(BinaryReader reader)
        {
            int length = reader.ReadByte();
            if ((length & 0x00000080) == 0x00000080)
            {
                int count = length & 0x0000000f;
                byte[] lengthBytes = new byte[4];
                reader.Read(lengthBytes, 4 - count, count);
                Array.Reverse(lengthBytes); //
                length = BitConverter.ToInt32(lengthBytes, 0);
            }
            return length;
        }
        #endregion
    }

}
